package com.zqweb.gulimall.product.service.impl;

import com.zqweb.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.zqweb.gulimall.product.dao.AttrDao;
import com.zqweb.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zqweb.gulimall.product.entity.AttrEntity;
import com.zqweb.gulimall.product.service.AttrService;
import com.zqweb.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.zqweb.gulimall.product.vo.AttrRelationVo;
import com.zqweb.gulimall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqweb.common.utils.PageUtils;
import com.zqweb.common.utils.Query;

import com.zqweb.gulimall.product.dao.AttrGroupDao;
import com.zqweb.gulimall.product.entity.AttrGroupEntity;
import com.zqweb.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrGroupDao attrGroupDao;
    @Autowired
    private AttrDao attrDao;
    @Autowired
    private AttrService attrService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        // 否则，需要带上key作特定的条件查询（携带输入参数---分组id、分组名，进行进一步准确查询）
        String key = (String)params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if(!StringUtils.isEmpty(key)){
            wrapper.and(obj -> {
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if(catId != 0){// 带上catId的条件查询，catId = 0则是无catId的全表查询
            wrapper.eq("catelog_id",catId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getAttrsByGroupId(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> entities = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>()
                .eq("attr_group_id", attrGroupId)
        );
        List<AttrEntity> attrEntities = entities.stream().map(entity -> {
            Long attrId = entity.getAttrId();
            AttrEntity attrEntity = attrDao.selectById(attrId);
            return attrEntity;
        }).collect(Collectors.toList());
        return attrEntities;
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrgroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> groupEntities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        if(groupEntities != null){
            List<AttrGroupWithAttrsVo> attrsVos = groupEntities.stream().map(groupEntity -> {
                Long groupId = groupEntity.getAttrGroupId();
//                AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(groupId);
                List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>()
                        .eq("attr_group_id", groupId));

                if (relationEntities != null && relationEntities.size() > 0) {
                    List<AttrEntity> attrEntities = relationEntities.stream().map(entity -> {
                        Long attrId = entity.getAttrId();
                        AttrEntity attrEntity = attrDao.selectById(attrId);
                        if(attrEntity != null){
                            return attrEntity;
                        }
                        return null;
                    }).filter(attrEntity -> attrEntity != null).collect(Collectors.toList());
                    AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
                    BeanUtils.copyProperties(groupEntity, attrsVo);
                    attrsVo.setAttrs(attrEntities);
                    return attrsVo;
                }
                return null;
            }).filter(groupEntity -> groupEntity != null).collect(Collectors.toList());
            return attrsVos;
        }
        return null;

    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuIdAndCatelogId(Long spuId, Long catalogId) {
        List<SpuItemAttrGroupVo> attrGroupVos = attrGroupDao.getAttrGroupWithAttrsBySpuIdAndCatelogId(spuId,catalogId);
        return attrGroupVos;
    }


}